{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "# 6.8 实现多分类交叉熵损失函数"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "在一个三分类任务中，假设有3个样本，对应的标签为[0,1,2]；经过模型计算，线性输出为："
   ]
  },
  {
   "cell_type": "raw",
   "id": "4298cb88-5c53-4f51-ac53-b99d7187ce33",
   "metadata": {},
   "source": [
    "[[1.2,3.4,5.],\n",
    " [2.2,4.6,5.],\n",
    " [3.2,8.3,5.]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14769303-221e-4882-ac7f-7175e7b372ce",
   "metadata": {},
   "source": [
    "要求：\n",
    "\n",
    "- 请使用多分类交叉熵损失函数计算模型的损失。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b624ebee-980f-4c1e-b963-a24ff0b669f6",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "使用Softmax激活函数计算出预测概率后，可以使用多分类交叉熵损失函数的计算公式计算出概率损失。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[0.01827278 0.16491213 0.816815  ]\n",
      " [0.03512738 0.38721526 0.57765734]\n",
      " [0.00584551 0.9587913  0.03536325]], shape=(3, 3), dtype=float32)\n",
      "平均交叉熵损失为： 2.7643998\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "# 4个样本的标签\n",
    "y=tf.constant([0,1,2],dtype=tf.int32)\n",
    "# 将标签转为独热编码\n",
    "Y=tf.one_hot(y,3)\n",
    "print(Y)\n",
    "# 通过Softmax函数计算4个样本分别属于3个类别的概率值\n",
    "# 线性输出\n",
    "Y_PRED=[\n",
    "          [1.2,3.4,5.],\n",
    "          [2.2,4.6,5.],\n",
    "          [3.2,8.3,5.]    \n",
    "         ]\n",
    "PRED=tf.nn.softmax(Y_PRED)\n",
    "print(PRED)\n",
    "Loss = -tf.reduce_sum(Y*tf.math.log(PRED))/3\n",
    "print(\"平均交叉熵损失为：\",Loss.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3b3e36e-8418-4caf-af51-d8ac80e2a321",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
